package com.hepinghospital.user.ui.activity.userinfo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.hepinghospital.user.R;
import com.hepinghospital.user.adapter.HelpListAdapter;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.ui.WebViewActivity;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.HomeDeptResponse;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import jz.joyoung.robot.util.DpUtil;

/**
 * 帮助中心
 *
 * @time 2021/1/6 14:45
 */

public class SettingActivity extends BaseActivity {
    @BindView(R.id.edt_search)
    TextView edtSearch;
    @BindView(R.id.rl_reply)
    RelativeLayout rlReply;
    @BindView(R.id.rv_question)
    RecyclerView rvQuestion;

    List<HomeDeptResponse> listHelp = new ArrayList<>();
    private HelpListAdapter helpListAdapter;

    @Override
    protected void initViews() {
        GridLayoutManager managerDept = new GridLayoutManager(this, 3);
        rvQuestion.setLayoutManager(managerDept);
        helpListAdapter = new HelpListAdapter(this, listHelp);
        rvQuestion.setAdapter(helpListAdapter);
        if (rvQuestion.getItemDecorationCount() == 0) {
            rvQuestion.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void onDraw(@NotNull Canvas c, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                    super.onDraw(c, parent, state);
                }

                @Override
                public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    outRect.top = DpUtil.dip2px(mContext, 10);
                    outRect.right = DpUtil.dip2px(mContext, 3);
                    outRect.left = DpUtil.dip2px(mContext, 3);
                    outRect.bottom = DpUtil.dip2px(mContext, 5);
                }
            });
        }
    }

    @Override
    protected void initData() {
        getHelpList();
    }


    @Override
    protected void doBeforeSetcontentView() {
        ImmersionBar.with(this).keyboardEnable(true).statusBarColor(R.color.mainBlue).fitsSystemWindows(true).navigationBarColor(R.color.black).autoDarkModeEnable(true).init();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_setting;
    }


    @OnClick({R.id.edt_search, R.id.rl_reply, R.id.rl_agree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edt_search:
                Intent intentOne = new Intent(this, SearchHelpActivity.class);
                intentOne.putExtra("helpName", "");
                intentOne.putExtra("helpCode", "");
                startActivity(intentOne);
                break;
            case R.id.rl_reply:
                //意见反馈
                Intent intent = new Intent(SettingActivity.this, UserInfoEditActivity.class);
                intent.putExtra("editType", "addFeedback");
//                intent.putExtra( "editData", BaseApplication.name );//上次保存过的数据
                startActivity(intent);
                break;
            case R.id.rl_agree:
                startActivity(new Intent(mContext, WebViewActivity.class).putExtra("fromWhere", "LoginActivity").putExtra("rulesType", "7"));
                break;
            default:
                break;
        }
    }

    private void getHelpList() {
        RestProxy.getInstance().getHelpList(new BaseObserver<List<HomeDeptResponse>>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onSuccess(List<HomeDeptResponse> result) {
                try {
                    listHelp.clear();
                    listHelp.addAll(result);
                    helpListAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    // 捕获异常，如果要捕获Json数据异常，写catch(JSONException e)
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(SettingActivity.this, e);
            }

        });
    }
}
